dojo.require("dijit.MenuBar");
dojo.require("dijit.PopupMenuBarItem");
dojo.require("dijit.MenuBarItem");
dojo.require("dijit.DropDownMenu");
dojo.require("dijit.MenuItem");
dojo.require("dijit.TitlePane");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dojo.store.Memory");
dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.form.CurrencyTextBox");
dojo.require("dijit.form.DropDownButton");
dojo.require("dijit.TooltipDialog");
dojo.requireLocalization("dojo.cldr", "currency", 'pt-br');
dojo.requireLocalization("dojo.cldr", "number", 'pt-br');

function mandatoryField(id) {
	var field = document.getElementById(id);
	if (field.value == "") {
		alert("Campo " + field.title + " preenchimento obrigatório!");
		field.value = field.value.trim();
		field.focus();
		return false;
	}
	return true;
}

function mandatoryRadio(id, acao) {
	var field = eval("document.forms[0]." + id);
	for (var i = 0; i < field.length; i++) {
		if (field[i].checked)
			return true;
	}
	alert("É necessário selecionar um item para " + acao + "!");
	return false;
}

function getSelectedRadio(id) {
	var field = eval("document.forms[0]." + id);
	for (var i = 0; i < field.length; i++) {
		if (field[i].checked)
			return field[i];
	}
}

function validValue(field, code) {
	field = document.getElementById(field);
	switch(code) {
		case 1: { //Tamanho Obrigatorio		
			if(field.value.length != 0 && field.value.length != field.maxLength) {
				alert("Campo " + field.title + " inválido! Favor verificar a correta digitado!");
				return false;
			}
			
		}
		default: {
			return true;
		}
	}
	
}

function setMethod(methodName) {
	var field = document.getElementById("method");
	if (field == null) {
		alert("Campo 'method' inexistente!");
		return false;
	}
	field.value = methodName;
}

function onFocus(field) {
	document.getElementById(field).focus();
}

function formataCampo(field, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen, bolMask, sCod, nTecla;

	if (document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if (document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	}

	sValue = field.value;

	// Limpa todos os caracteres de formata��o que
	// j� estiverem no campo.
	sValue = sValue.toString().replace("-", "");
	sValue = sValue.toString().replace("-", "");
	sValue = sValue.toString().replace(".", "");
	sValue = sValue.toString().replace(".", "");
	sValue = sValue.toString().replace("/", "");
	sValue = sValue.toString().replace("/", "");
	sValue = sValue.toString().replace("(", "");
	sValue = sValue.toString().replace("(", "");
	sValue = sValue.toString().replace(")", "");
	sValue = sValue.toString().replace(")", "");
	sValue = sValue.toString().replace(" ", "");
	sValue = sValue.toString().replace(" ", "");
	sValue = sValue.toString().replace(":", "");
	fldLen = sValue.length;
	mskLen = sMask.length;

	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask
				.charAt(i) == "/"))
		bolMask = bolMask
				|| ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask
						.charAt(i) == " "))
		bolMask = bolMask || (sMask.charAt(i) == ":")

		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++;
		} else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}

		i++;
	}

	field.value = sCod;

	if (nTecla != 8) { // backspace
		if (sMask.charAt(i - 1) == "9") { // apenas n�meros...
			return ((nTecla > 47) && (nTecla < 58));
		} // n�meros de 0 a 9
		else { // qualquer caracter...
			return true;
		}
	} else {
		return true;
	}
}

function validateEmail(field) {
	$email = document.getElementById(field);
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(!emailReg.test($email.value)) {
		alert('Email Inválido!');
		$email.focus();
		return false;
	} else {
		return true;
	}
}

function validatePhone(field) {
	$phone = document.getElementById(field);
	if($phone.value.length != 13) {
		alert('Número de Telefone Inválido!');
		$phone.focus();
		return false;
	}
	return true;
}

function validateCNPJ(field){
    CNPJ = document.getElementById(field).value;
    if(!CNPJ){return false;}
    erro = new String;
    if(CNPJ == "00.000.000/0000-00") {
    	erro = "CNPJ inválido";
    }
    CNPJ = CNPJ.replace(".","");
    CNPJ = CNPJ.replace(".","");
    CNPJ = CNPJ.replace("-","");
    CNPJ = CNPJ.replace("/","");

    var a = [];
    var b = new Number;
    var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
    for(var i=0; i<12; i++){
      a[i] = CNPJ.charAt(i);
      b += a[i] * c[i+1];
    }
    if((x = b % 11) < 2){
      a[12] = 0;
    }else{
      a[12] = 11-x;
    }
    b = 0;
    for(var y=0; y<13; y++){
      b += (a[y] * c[y]);
    }
    if((x = b % 11) < 2){
      a[13] = 0;
    }else{
      a[13] = 11-x;
    }
    if((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){ erro ="CNPJ inválido!";}
    if(erro.length > 0){
      alert(erro);
      document.getElementById(field).focus();
      return false;
    }
    return true;
}

function formatar_moeda(campo, separador_milhar, separador_decimal, tecla) {
	
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? tecla.which : tecla.keyCode;

	if (whichCode == 13) return true; // Tecla Enter
	if (whichCode == 8) return true; // Tecla Delete
	
	key = String.fromCharCode(whichCode); // Pegando o valor digitado
	if (whichCode < 48 || whichCode > 57 && whichCode < 96 || whichCode > 105) return false; // Valor inválido (não inteiro)
	len = campo.value.length;
	for(i = 0; i < len; i++)
	if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != separador_decimal)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ separador_decimal + '0' + aux;
	if (len == 2) campo.value = '0'+ separador_decimal + aux;

	if (len > 2) {
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += separador_milhar;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}

		campo.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		campo.value += aux2.charAt(i);
		campo.value += separador_decimal + aux.substr(len - 2, len);
	}

	return false;
}

function autoTab(input, e)  {   
  var ind = 0;  
  var isNN = (navigator.appName.indexOf("Netscape")!=-1);  
  var keyCode = (isNN) ? e.which : e.keyCode;   
  var nKeyCode = e.keyCode;   
  if(keyCode == 13){   
    if (!isNN) {window.event.keyCode = 0;} // evitar o beep  
    ind = getIndex(input);  
    if (input.form[ind].type == 'textarea') {  
      return;  
    }  
    ind++;  
    input.form[ind].focus();   
    if (input.form[ind].type == 'text') {  
      input.form[ind].select();   
    }  
  }   
  
  function getIndex(input) {   
    var index = -1, i = 0, found = false;   
    while (i < input.form.length && index == -1)   
      if (input.form[i] == input) {  
        index = i;  
          if (i < (input.form.length -1)) {  
           if (input.form[i+1].type == 'hidden') {  
       index++;   
     }  
     if (input.form[i+1].type == 'button' && input.form[i+1].id == 'tabstopfalse') {  
       index++;   
     }  
   }  
      }  
      else   
   i++;   
    return index;   
  }  
} 
